package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.ViewDao;
import model.View;
import util.CharSet;

/**
 * Servlet implementation class ViewServlet
 */
@WebServlet("/ViewServlet")
public class ViewServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String handle = request.getParameter("handle");
		if(handle!=null && handle.equals("delView")) {
			String vname = CharSet.getNewString(request.getParameter("vname"));
			View view;
			try {
				if(ViewDao.getViewNoCacheForBool(vname)) {
					view = ViewDao.getViewNoCache(vname);
					ViewDao.delView(view);
					String flag = "1";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminViewList.jsp").forward(request, response);
				}else {
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminViewList.jsp").forward(request, response);
				}
			} catch (Exception e) {
				String flag = "0";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminViewList.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
